package com.bleeth.fx.component;

import cn.hutool.core.util.StrUtil;
import com.bleeth.fx.helper.SStyleHelper;
import com.bleeth.fx.helper.ThemeHelper;
import com.bleeth.fx.helper.ThemeTypeEnum;
import javafx.scene.control.Button;
import javafx.scene.shape.Circle;
import lombok.Builder;


/**
 * @author Sakura
 * @version 1.0
 * @date 2021-11-13 22:17
 **/
@Builder
public class MButton {

    @Builder.Default
    private String type = "DEFAULT";

    @Builder.Default
    private boolean plain = false;

    @Builder.Default
    private boolean round = false;

    private String icon;

    @Builder.Default
    private boolean disabled = false;

    @Builder.Default
    private boolean circle = false;

    @Builder.Default
    private boolean loading = false;

    @Builder.Default
    private double width = DEFAULT_WIDTH;

    @Builder.Default
    private double height = DEFAULT_HEIGHT;

    private String text;

    private Button button;

    private static final double DEFAULT_WIDTH = 95;
    private static final double DEFAULT_HEIGHT = 40;
    private static final double DEFAULT_RADIUS = 5;


    public Button make() {
        button = new Button();
        button.setPrefWidth(width);
        button.setPrefHeight(height);

        if (StrUtil.isNotEmpty(text)) {
            button.setText(text);
        }

        if (StrUtil.isNotEmpty(icon)) {
            button.setGraphic(MIcon.builder().icon(icon).size(height / 2).build().make());
        }

        SStyleHelper.node(button)
                .addStyle("-fx-background-radius", 5)
                .addStyle("-fx-font-size", "14px");


        if (round) {
            SStyleHelper.node(button)
                    .addStyle("-fx-background-radius", height / 2);
        }

        if (circle) {
            width = height;
            button.setPrefSize(width, height);
            button.setClip(new Circle(height / 2, width / 2, height / 2));
        }

        if (disabled) {
            button.setDisable(disabled);
        }

        if (StrUtil.equalsIgnoreCase(ThemeHelper.DEFAULT, type)) {
            SStyleHelper.node(button)
                    .addHoverStyle("-fx-background-color", ThemeTypeEnum.PRIMARY_COLOR.getValue() + "22")
                    .addHoverStyle("-fx-text-fill", ThemeTypeEnum.PRIMARY_COLOR.getValue())
                    .apply();
        }
        if (StrUtil.equalsIgnoreCase(ThemeHelper.PRIMARY, type)) {
            SStyleHelper.node(button)
                    .addStyle("-fx-background-color", ThemeTypeEnum.PRIMARY_COLOR.getValue())
                    .addStyle("-fx-text-fill", ThemeTypeEnum.WHITE_COLOR.getValue())
                    .addHoverStyle("-fx-background-color", ThemeTypeEnum.PRIMARY_COLOR.getValue() + "AA")
                    .apply();
        }
        if (StrUtil.equalsIgnoreCase(ThemeHelper.INFO, type)) {
            SStyleHelper.node(button)
                    .addStyle("-fx-background-color", ThemeTypeEnum.INFO_COLOR.getValue())
                    .addStyle("-fx-text-fill", ThemeTypeEnum.WHITE_COLOR.getValue())
                    .addHoverStyle("-fx-background-color", ThemeTypeEnum.INFO_COLOR.getValue() + "AA")
                    .apply();
        }
        if (StrUtil.equalsIgnoreCase(ThemeHelper.WARNING, type)) {
            SStyleHelper.node(button)
                    .addStyle("-fx-background-color", ThemeTypeEnum.WARNING_COLOR.getValue())
                    .addStyle("-fx-text-fill", ThemeTypeEnum.WHITE_COLOR.getValue())
                    .addHoverStyle("-fx-background-color", ThemeTypeEnum.WARNING_COLOR.getValue() + "AA")
                    .apply();
        }
        if (StrUtil.equalsIgnoreCase(ThemeHelper.DANGER, type)) {
            SStyleHelper.node(button)
                    .addStyle("-fx-background-color", ThemeTypeEnum.DANGER_COLOR.getValue())
                    .addStyle("-fx-text-fill", ThemeTypeEnum.WHITE_COLOR.getValue())
                    .addHoverStyle("-fx-background-color", ThemeTypeEnum.DANGER_COLOR.getValue() + "AA")
                    .apply();
        }
        if (StrUtil.equalsIgnoreCase(ThemeHelper.SUCCESS, type)) {
            SStyleHelper.node(button)
                    .addStyle("-fx-background-color", ThemeTypeEnum.SUCCESS_COLOR.getValue())
                    .addStyle("-fx-text-fill", ThemeTypeEnum.WHITE_COLOR.getValue())
                    .addHoverStyle("-fx-background-color", ThemeTypeEnum.SUCCESS_COLOR.getValue() + "AA")
                    .apply();
        }

        String pressBackgroundStyle = SStyleHelper.getStyle(button,"-fx-background-color");
        SStyleHelper.node(button)
                .addPressStyle("-fx-background-color", StrUtil.isEmpty(pressBackgroundStyle) ? "FFFFFF55" : pressBackgroundStyle + "55")
                .apply();


        if (plain) {
            //hover 和 默认背景色互换  字体颜色为背景色
            String style = SStyleHelper.getStyle(button,"-fx-background-color");
        }
        return button;
    }

}
